package code
{
	import code.ws_common_ems.ModeRenderEms;
	import code.ws_common_interface.SerManager;
	import code.ws_common_interface.ctrl.AndriodAppAlertCtrl;
	import code.ws_common_scene.manager.SceneManager;
	import code.ws_common_scene.map.MapModePool;
	import code.ws_common_scene.map.XMapMode;
	import code.ws_common_scene.texturedata.EftTextureDataPool;
	import code.ws_common_scene.texturedata.ModeTextureDataPool;
	import code.ws_common_utils.EnterFrameUtils;
	import code.ws_common_utils.LayerUtils;
	import code.ws_common_utils.UrlUtils;
	
	import je.engine.dicresource.JEResourceDic;
	import je.engine.loaderware.loa.JELoaMain;
	import je.engine.pub.JEPubObj;
	import je.engine.pub.evts.JELoaEvt;
	import je.engine.pub.obj.JELoaPubObj;
	
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.utils.AssetManager;

	
	/**
	 * Starling主函数 
	 * @author qiangchen
	 * 
	 */	
	public class StarlingMain extends Sprite
	{
		/** 静态属性 */
		public static var instance:StarlingMain;
		/** 资源管理器 */
		public  var assets:AssetManager = new AssetManager( -1, false );
		/** 退出控制器 */
		public var alert:AndriodAppAlertCtrl;
		
		/**
		 * 构造函数 
		 * 
		 */		
		public function StarlingMain()
		{
			instance = this;
			this.addEventListener( Event.ADDED_TO_STAGE, thisAdd_handler );
		}
		
		private function thisAdd_handler( evt:Event ):void
		{
			// 初始化游戏层级
			LayerUtils.ini();
			
			// 本地预加载过场背景图以及ui纹理、配置
			interludeFirst();
		}
		
		
		/**
		 * 预加载ui纹理集以及过场背景
		 * 
		 */		
		private function interludeFirst():void
		{
			var uis:Array = UrlUtils.guiUrl;
			assets.enqueue( uis[0], uis[1],  UrlUtils.getOtherResUrl("interludeBg","png") );
			assets.loadQueue( interludeProFn );
			uis.length = 0;
			uis = null;
		}
		
		
		/** 
		 * 预加载完毕
		 * @param radio
		 * 
		 */		
		private function interludeProFn( radio:Number ):void
		{
			if( radio == 1 )
			{
				// 释放logo图
				ws_mobile._instance.removeLogo();
				
				// 初始化服务类
				SerManager.instance;
				
				// 应用退出控制器
				alert = new AndriodAppAlertCtrl();
				
				// 初始化加载
				JELoaPubObj.instance.addEventListener(JELoaEvt.LOADER_PROGRESS, progressHandler );
				JELoaPubObj.instance.addEventListener(JELoaEvt.LOADER_COMPLETE, comHandler );
				JELoaMain.startLoader( UrlUtils.mainConfig );
				LayerUtils.interlude.visible = true;
				SerManager.instance.interludeLoginSer.setProgressInfo( "Initialization... " );
			}
		}
		
		
		private function progressHandler( evt:JELoaEvt ):void
		{
			var ary:Array = evt.paramobj as Array;
			SerManager.instance.interludeLoginSer.setProgerssVal( ary[2] );
			ary.length = 0;
			ary = null;
		}
		
		
		/**
		 * 游戏初始化资源完毕 
		 * @param evt
		 * 
		 */		
		private function comHandler( evt:JELoaEvt ):void
		{
			JELoaPubObj.instance.removeEventListener(JELoaEvt.LOADER_PROGRESS, progressHandler );
			JELoaPubObj.instance.removeEventListener(JELoaEvt.LOADER_COMPLETE, comHandler );
			JEPubObj.delClass(JELoaMain);
			JEPubObj.delClass(JELoaPubObj);
			
			// ...解析模型动作配置
			ModeTextureDataPool.iniAnalyze( ModeRenderEms.HERO, XML(JEResourceDic.getRes("heroActions")._$content) );
			JEResourceDic.removeRes("heroActions");
			ModeTextureDataPool.iniAnalyze( ModeRenderEms.MONSTER, XML(JEResourceDic.getRes("monsterActions")._$content ) );
			JEResourceDic.removeRes("monsterActions");				
			
			// ...解析特效动作配置
			EftTextureDataPool.analyze( XML(JEResourceDic.getRes("eftActions")._$content) );
			JEResourceDic.removeRes("eftActions");
			
			// ...解析地图cm文件，并进入游戏场景
			MapModePool.analyze( XML( JEResourceDic.getRes("map")._$content ), initOk );
			JEResourceDic.removeRes("map");
		}
		
		
		/**
		 * 进入游戏场景
		 * 
		 */		
		private function initOk():void
		{
			LayerUtils.interlude.visible = false;
			
			// 初始化场景管理器，全局监听事件
			SceneManager.instance;
			this.addEventListener(Event.ENTER_FRAME, refresh );
			
			// 进入场景
			var xmapMode:XMapMode = MapModePool.getMapMode( "1002" );
			SceneManager.instance.scene.setEnterMapPos( xmapMode._heroIniPos );
			SceneManager.instance.enterMap( xmapMode._mapId );
			xmapMode = null;
		}
		
		/**
		 * 实时刷新 
		 * @param evt
		 * 
		 */		
		private function refresh( evt:Event ):void
		{
			for each( var fn:Function in EnterFrameUtils.fnDic )
			{
				fn && fn();
			}
		}
	}
}